package com.companyName.sample.controller.controller;

import com.companyName.sample.common.base.CommBaseController;
import com.companyName.sample.common.bean.ResponseData;
import com.companyName.sample.common.constant.ResponseMsg;
import com.companyName.sample.common.util.BeanValidation;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.servlet.ModelAndView;


public abstract class BaseController extends CommBaseController{


    /**
     * 失败的请求返回 -- 返回 String
     */
    public ResponseEntity<String> errorResponse(HttpStatus code) {
        return errorResponse(code, "");
    }

    /**
     * 失败的请求返回 -- 返回 String
     */
    public ResponseEntity<String> errorResponse(HttpStatus code, String message) {
        return new ResponseEntity<String>(message, code);
    }

    /**
     * 失败的请求返回 -- 返回 ResponseData对象
     */
    @SuppressWarnings("rawtypes")
    public ResponseEntity<ResponseData> failureResponse(int code, Object... result) {
        HttpHeaders headers = new HttpHeaders();
        return new ResponseEntity<ResponseData>(super.baseFailureResponse(code, result), headers, HttpStatus.OK);
    }




    /**
     * 成功的请求返回
     */
    @SuppressWarnings("rawtypes")
    public ResponseEntity<ResponseData> successResponse(Object... result) {
        HttpHeaders headers = new HttpHeaders();
        return new ResponseEntity<ResponseData>(super.baseSuccessResponse(result), headers, HttpStatus.OK);
    }
    /**
     * 对返回结果进行封装  -- 返回成功状态
     */
    @SuppressWarnings("rawtypes")
    public ResponseEntity<ResponseData> wrapResponseSuccess(ResponseData<?> result) {
        return wrapResponse(result, HttpStatus.OK);
    }

    /**
     * 对返回结果进行封装  -- 返回错误状态
     */
    @SuppressWarnings("rawtypes")
    public ResponseEntity<ResponseData> wrapResponseFailure(ResponseData<?> result) {
        return wrapResponse(result, HttpStatus.BAD_REQUEST);
    }

    /**
     * 对返回结果进行封装
     */
    @SuppressWarnings("rawtypes")
    public ResponseEntity<ResponseData> wrapResponse(ResponseData<?> result, HttpStatus failureStatus) {
        HttpHeaders headers = new HttpHeaders();
        HttpStatus status = result.success() ? HttpStatus.OK : failureStatus;
        return new ResponseEntity<ResponseData>(result, headers, status);
    }

    /**
     * 对返回结果进行封装 -- 抛出异常的处理
     */
    public void wrapExceptionResponseData(ModelAndView model,String msg) {
        ResponseData rd = new ResponseData(ResponseMsg.EXCEPTION.getCode(),null,msg);
        model.addObject(rd);
    }

    /**
     * Bean校验
     */
    public ResponseData validateResponse(Object bean) {
        HttpHeaders headers = new HttpHeaders();
        String msg =  BeanValidation.validate(bean);
        if(!"".equals(msg)) {
            return new ResponseData(ResponseMsg.VALIDATE_EXCEPTION.getCode(), null,msg);
        }
        return null;
    }

    /**
     * 对返回结果进行封装  -- 返回成功状态
     */
    @SuppressWarnings("rawtypes")
    public ModelAndView wrapModelAndFailure( ModelAndView model,ResponseData rd) {
        model.addObject(rd.getMessage());
        model.setViewName("userInfo");
        return model;
    }
}
